import { defineStore } from "pinia";
import { ref } from "vue";

// 生产存储数据
export const useProduceStore = defineStore("produce", () => {
  // state
  // 加工单数据
  const workOrderList = ref(uni.getStorageSync("workOrderList") || "[]");
  // 冷铁加工单数据
  const chillOrderList = ref(uni.getStorageSync("chillOrderList") || "[]");
  // 班组登录成员数据
  const crewMemberList = ref(uni.getStorageSync("crewMemberList") || "[]");
  // 非生产报工明细数据
  const planAddDetailList = ref(
    uni.getStorageSync("planAddDetailList") || "[]"
  );
  // action
  /**
   * 设置加工单数据
   */
  function setWorkOrderList(data) {
    workOrderList.value = JSON.stringify([data]);
    uni.setStorageSync("workOrderList", workOrderList.value);
  }
  /**
   * 设置冷铁加工单数据
   */
  function setChillOrderList(data) {
    chillOrderList.value = JSON.stringify([data]);
    uni.setStorageSync("chillOrderList", chillOrderList.value);
  }
  /**
   * 设置班组登录成员数据
   */
  function setCrewMemberList(data: any) {
    if (Array.isArray(data)) {
      crewMemberList.value = JSON.stringify(data);
    } else {
      crewMemberList.value = JSON.stringify([data]);
    }
    uni.setStorageSync("crewMemberList", crewMemberList.value);
  }
  function setPlanAddDetailList(data: any) {
    planAddDetailList.value = JSON.stringify(data);
    uni.setStorageSync("planAddDetailList", planAddDetailList.value);
  }
  return {
    workOrderList,
    setWorkOrderList,
    chillOrderList,
    setChillOrderList,
    crewMemberList,
    setCrewMemberList,
    planAddDetailList,
    setPlanAddDetailList,
  };
});
